package com.newgoldcurrency.utils.network;

import android.os.StrictMode;

import com.newgoldcurrency.BuildConfig;
import com.newgoldcurrency.utils.CancelCall;
import com.newgoldcurrency.utils.Tools;

import java.util.HashMap;
import java.util.Set;

import okhttp3.CacheControl;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class Network {
    public static void post(HashMap<String, String> map, String url, CancelCall cancelCall){
        if (Tools.isWifiProxy()){
            Tools.toast("Network malicious agent is detected and all network requests are terminated");
        }else{
            CacheControl cacheControl = new CacheControl.Builder().noCache().noStore().build();
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
            FormBody.Builder body = new FormBody.Builder();
            Set<String> keys = map.keySet();
            for (String key :keys) body.add(key,map.get(key));
            cancelCall.sendCall(getClient().newCall(new Request.Builder().cacheControl(cacheControl).url(BuildConfig.BASE_URL + url).post(body.build()).build()));
        }
    }
    private static OkHttpClient getClient() {
       return new OkHttpClient().newBuilder().addInterceptor(new ResponseContentInterceptor()).build();
    }
}